package db.tables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.db.Table;

import db.beans.T_DictionaryBean;

public class T_DictionaryTable extends Table {
	public static final String COL_SRC_KEY_WORD = "SRC_KEY_WORD";
	public static final String COL_DES_KEY_WORD = "DES_KEY_WORD";
	public static final String COL_DELETE_FLAG = "DELETE_FLAG";

	public static T_DictionaryBean[] selectAll(Connection conn, String tableName) throws SQLException {
		final String sql = "SELECT "
							+ COL_SRC_KEY_WORD
							+ "," + COL_DES_KEY_WORD
							+ "," + COL_DELETE_FLAG
							+ " FROM " + tableName;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Vector<T_DictionaryBean> v = new Vector<T_DictionaryBean>();
		try {
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			int i;
			while (rs.next()) {
				i = 0;
				T_DictionaryBean rec = new T_DictionaryBean();
				rec.setSrcKeyWord(rs.getString(++i));
				rec.setDesKeyWord(rs.getString(++i));
				rec.setDeleteFlag(rs.getInt(++i) == DELETE_FLAG);
				v.add(rec);
			}
		} finally {
			if (rs != null) {
				rs.close();
			}

			if (stmt != null) {
				stmt.close();
			}
		}
		return (T_DictionaryBean[]) v.toArray(new T_DictionaryBean[v.size()]);
	}
	public static String selectDesKeywordBySrcKeyword(Connection conn, String tableName, String srcKeyword) throws SQLException {
		final String sql = "SELECT " + COL_DES_KEY_WORD
				+ " FROM " + tableName
				+ " WHERE "
				+ COL_SRC_KEY_WORD + "=?"
				+ " AND " + COL_DELETE_FLAG + "=" + NOT_DELETE_FLAG
				;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		String desKeyword = "";
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, srcKeyword);
			rs = stmt.executeQuery();
			if (rs.next()) {
				desKeyword = rs.getString(1);
			}
		} finally {
			if (rs != null) {
				rs.close();
			}

			if (stmt != null) {
				stmt.close();
			}
		}
		return desKeyword;
	}
}
